package homework.valid.validators.impl;

import homework.valid.validators.BaseValidator;

import java.math.BigDecimal;

public class RangeValidator extends BaseValidator {

    public final static long INF = Long.MAX_VALUE;
    private BigDecimal min;
    private BigDecimal max;

    public RangeValidator(String msg, BigDecimal min, BigDecimal max) {
        super(msg);
        this.min = min;
        this.max = max;
    }

    public RangeValidator(String msg, long min, long max) {
        this(msg, new BigDecimal(min), new BigDecimal(max));
    }

    public RangeValidator(String msg, double min, double max) {
        this(msg, new BigDecimal(min), new BigDecimal(max));
    }

    public boolean isValid(BigDecimal v) {
        return v.compareTo(min) >= 0 && v.compareTo(max) <= 0;
    }

    @Override
    public boolean isValid(Object v) {
        if(v instanceof Double) {
            return isValid(new BigDecimal((double)v));
        } else if(v instanceof Long) {
            return isValid(new BigDecimal((long)v));
        } else if(v instanceof Integer) {
            return isValid(new BigDecimal((int)v));
        } else if(v instanceof Float) {
            return isValid(new BigDecimal((float)v));
        }
        return false;
    }
}
